package com.superc.lib.network.implement.superp;

import java.io.IOException;
import java.net.SocketAddress;
import java.util.Map;

import com.superc.lib.network.inteface.superp.IClientHandler;
import com.superc.lib.network.inteface.superp.IProtocolEventSink;

public class SuperClient implements IProtocolEventSink,IClientHandler {

	
	private IProtocolEventSink eventSink=null; 
	SuperProtocolHandler handler=null;
	
	@Override
	public void setEventSink(IProtocolEventSink sink) {
		this.eventSink=sink;
		if(handler!=null) handler.setEventSink(eventSink);
	}

	@Override
	public IProtocolEventSink getEventSink() {
		return eventSink;
	}

	@Override
	public void connect(String address, int port) throws IOException {
		if(handler==null) handler=new SuperProtocolHandler();
		handler.connect(address, port);
	}

	@Override
	public void sendCommand(Byte command, String parameter) throws IOException {
		if(handler!=null) handler.sendCommand(command, parameter);
	}

	@Override
	public void sendCommand(Byte command, String parameter, boolean compressed)
			throws IOException {
		if(handler!=null) handler.sendCommand(command, parameter,compressed);
	}

	@Override
	public void addCommandDescription(byte commandId, String description) {
		if(handler!=null) handler.addCommandDescription(commandId, description);
	}

	@Override
	public String getCommandDescription(byte commandId) {
		if(handler!=null) return handler.getCommandDescription(commandId);
		return null;
	}

	@Override
	public Map<Byte, String> getCommandDescriptions() {
		if(handler!=null) return handler.getCommandDescriptions();
		return null;
	}

	@Override
	public void addCommandDescriptions(Map<Byte, String> commands) {
		if(handler!=null) handler.addCommandDescriptions(commands);
	}

	@Override
	public void close() {
		if(handler!=null) handler.close();
	}

	@Override
	public boolean getDataCompressed() {
		if(handler!=null) return handler.getDataCompressed();
		return false;
	}

	@Override
	public void setDataCompressed(boolean isCompressed) {
		if(handler!=null) handler.setDataCompressed(isCompressed);
	}

	@Override
	public void ClientConnected(SocketAddress clientAddress, Boolean bolRejected,int handleId) {
		
	}

	@Override
	public void CommandReceived(byte commandId,String commandName, String parameter,int handleId) {
		if(eventSink!=null) eventSink.CommandReceived(commandId, commandName,parameter,handleId);
	}

	@Override
	public void StreamDataReceived(byte[] datas,int startIndex, int dataLength, int handleId) {
		if(eventSink!=null) eventSink.StreamDataReceived(datas,startIndex, dataLength, handleId);
	}

	@Override
	public void sendStream(byte[] datas, int startIndex, int length ) throws IOException {
		if(handler!=null) handler.sendStream(datas, startIndex, length );
	}

	@Override
	public void sendStream(byte[] datas, int startIndex, int length,
			 boolean isCompressed) throws IOException {
		if(handler!=null) handler.sendStream(datas, startIndex, length, isCompressed);
	}

	@Override
	public void ConnectionClosed(int handleId) {
		if(eventSink!=null) eventSink.ConnectionClosed(handleId);
	}

	@Override
	public void ErrorHappened(Exception exp,int handleId) {
		if(eventSink!=null) eventSink.ErrorHappened(exp,handleId);
	}

	
}
